package common.customer.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Collect;
import common.customer.bean.Customer;
import common.customer.service.CollectService;

public class CollectAction extends ActionSupport {

	/**
	 * auto generated
	 */
	private static final long serialVersionUID = 1L;
	private CollectService collectService;
	
	//添加收藏
	private String url;
	private String description;
	private String resultStr;
	private int keyId;
	private String type;
	private String resultCode;
	public String addCollect(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(keyId<=0){
			resultCode="0";
			resultStr ="keyID为空,联系管理员";
			return SUCCESS;
		}
		if(type==null || "".equals(type.trim())){
			resultCode="0";
			resultStr ="type为空,联系管理员";
			return SUCCESS;
		}
		if(description==null || "".equals(description.trim())){
			resultCode="0";
			resultStr ="描述为空,联系管理员";
			return SUCCESS;
		}
		if(url==null || "".equals(url.trim())){
			resultCode="0";
			resultStr ="收藏地址为空,联系管理员";
			return SUCCESS;
		}
		if(user==null){
			resultCode="0";
			resultStr ="用户尚未登录";
		}else{
			if(this.collectService.testExistCollect(user.getId(),keyId, type)){
				resultCode="0";
				resultStr ="已经收藏,无需再次收藏";
				return SUCCESS;
			}
			
			
			Collect collect = new Collect();
			collect.setDescription(description);
			collect.setUrl(url);
			collect.setUser(user);
			collect.setKeyId(keyId);
			collect.setType(type);
			boolean result = this.collectService.addCollect(collect);
			if(result){
				resultCode="1";
				resultStr ="收藏成功";
			}else{
				resultCode="0";
				resultStr ="收藏失败,联系管理员";
			}
		}
		return SUCCESS;
	}
	//====================getter and setter method
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public CollectService getCollectService() {
		return collectService;
	}
	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}
	public int getKeyId() {
		return keyId;
	}
	public void setKeyId(int keyId) {
		this.keyId = keyId;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
}
